package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.itheima.health.mapper.OrderSettingMapper;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.poi.DateUtils;
import com.itheima.health.vo.OrderSettingVo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {
    @Override
    public Map findSettingData(int year, int month) {
        //设置日期的范围
        String beginDate = year + "-"+month+"-01";
        String endDate = year + "-"+month+"-31";
        List<OrderSetting> orderSettingList = baseMapper.findOrderSetting(beginDate,endDate);
        Map map = new HashMap();
        if(orderSettingList!=null&&orderSettingList.size()!=0){
            for (OrderSetting orderSetting : orderSettingList) {
                String dateString = DateUtils.parseDate2String(orderSetting.getOrderDate());
                //将最大预约数和已经预约数设置到VO中
                OrderSettingVo settingVo = new OrderSettingVo();
                settingVo.setNUMBER(orderSetting.getNumber());
                settingVo.setRESERVATIONS(orderSetting.getReservations());
                map.put(dateString,settingVo);
            }
        }
        return map;
    }



    //更新设置中的最大预约数
    @Override
    public Boolean updateNumberByOrderDate(int number, String orderdate) {
       return baseMapper.updateNumberByOrderdate(number,orderdate);
    }


    @Override
    public int isOrderOkByOrderDate(String orderDate) {
        return 0;
    }

    @Override
    public void updateReservationsByOrderDate(String orderDate) {

    }

    //excel表格上传
    @Override
    public void importOrderSettings(List<String[]> readExcel) {
        //数据类型的转化
       List<OrderSetting> orderList =  changeStringArrayToList(readExcel);
       //判断数据库中的日期和上传的表中的日期是否相同
            for (OrderSetting orderSetting : orderList) {
                QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("ORDERDATE",orderSetting.getOrderDate());//查询条件，日期是否相等
                //日期是否相等这一行数据
                OrderSetting selectOne = baseMapper.selectOne(queryWrapper);
                if(selectOne!=null){
                    //日期相等,将数据库中的记录id赋值给更新的数据进行更新
                    orderSetting.setId(selectOne.getId());
                }
            }
        //批量更新和添加
        saveOrUpdateBatch(orderList);

        }

    @Override
    public boolean updatesetmealById(int number, String settingDay) {
        return false;
    }

    @Override
    public int findOrderSettingByOrderDate(String orderDate) {

        return baseMapper.findOrderSettingByOrderDate(orderDate);
    }

    @Override
    @LcnTransaction
    public int updateOrderSettingByOrderDate(String orderDate) {
     return baseMapper.updateOrderSettingByOrderDate(orderDate);

    }


    //将数组转化为集合，并将数据封装到集合中
    private List<OrderSetting> changeStringArrayToList(List<String[]> readExcel) {
        List<OrderSetting> orderSettings = new ArrayList<>();
        if(readExcel!=null&&readExcel.size()!=0){
            for (String[] strings : readExcel) {
                OrderSetting orderSetting = new OrderSetting();
                //将日期格式转化为字符串格式
                Date orderDate = orderSetting.getOrderDate();
                String dateString  = DateUtils.parseDate2String(orderDate);
                orderSetting.setReservations(0);
                orderSetting.setNumber(Integer.parseInt(strings[1]));
                orderSetting.setOrderDate(DateUtils.parseString2Date(strings[0],"yyyy/MM/dd"));
                orderSettings.add(orderSetting);
            }
            //一定注意，不能在循环里面，否则只有一条数据
            return orderSettings;
        }
        return null;
    }
}
